package com.grape.ciq.api.redbook.internal.parse.xml;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
/**
 * XML Object parse
 * 
 * @author hantongshan
 * @modify time : 2014-05-17 17:42 PM
 */
public final class JAXBXMLParser {

	private static String xmlCoding = "UTF-8";

	private static Marshaller createMarshallerByType(Class<? extends Object> type) {
		Marshaller mar = null;
		try {
			JAXBContext jax = JAXBContext.newInstance(type);
			mar = jax.createMarshaller();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mar;
	}

	private static Unmarshaller createUnMarshallerByType(Class<?> type) {
		Unmarshaller unMar = null;
		try {
			JAXBContext jax = JAXBContext.newInstance(type);
			unMar = jax.createUnmarshaller();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return unMar;
	}

	/**
	 * parse XML Object to XML String
	 * 
	 * @param obj
	 * @return
	 */
	public static String parseObjectXmlData(Object obj) {
		String backXml = null;
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			Marshaller mar = createMarshallerByType(obj.getClass());
			mar.marshal(obj, baos);
			backXml = baos.toString();
			backXml = new String(baos.toByteArray(), xmlCoding);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return backXml;
	}

	/***
	 * if enCoding is null ,the
	 * 
	 * @param obj
	 * @param enCoding
	 * @return
	 */
	public static String parseObjectXmlData(Object obj, String enCoding) {
		String backXml = null;
		try {
			if (enCoding != null)
				xmlCoding = enCoding;
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			Marshaller mar = createMarshallerByType(obj.getClass());
			mar.marshal(obj, baos);
			backXml = baos.toString();
			backXml = new String(baos.toByteArray(), xmlCoding);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return backXml;
	}

	/**
	 * 
	 * default xmlCoding is UTF-8
	 * 
	 * @param type
	 * @param xmlData
	 * @return
	 */
	public static Object parseXmlDataObject(Class<?> type, String xmlData) {
		if (xmlData == null || xmlData.trim().length() == 0)
			return null;
		try {
			Unmarshaller unMar = createUnMarshallerByType(type);
			ByteArrayInputStream bais = new ByteArrayInputStream(
					xmlData.getBytes(xmlCoding));
			return unMar.unmarshal(bais);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Object parseXmlDataObject(Class<?> type, String xmlData,
			String enCoding) {
		if (xmlData == null || xmlData.trim().length() == 0)
			return null;
		try {
			xmlCoding = enCoding;
			Unmarshaller unMar = createUnMarshallerByType(type);
			ByteArrayInputStream bais = new ByteArrayInputStream(
					xmlData.getBytes(xmlCoding));
			return unMar.unmarshal(bais);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
}